package com.ctg.itrdc.imageconfig.service.impl;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONArray;
import com.ctg.itrdc.imageconfig.service.IImageConfigService;
import com.ctg.itrdc.imageconsole.data.config.dao.IImageConfigDao;
import com.ctg.itrdc.imageconsole.data.dto.imageconfig.ImageConfigDto;

import com.ctg.itrdc.imageconsole.data.entity.ImageConfig;
import com.ctg.itrdc.imageconsole.data.entity.ImageConfigName;
import com.ctg.itrdc.imageconsole.data.entity.ImageConfigSet;
import com.ctg.itrdc.imageconsole.data.entity.ImageConfigVersion;
import com.ctg.itrdc.pasp.security.utils.WebUtils;
import com.ctg.itrdc.paspsdk.common.entity.PageInfo;

@Component("imageConfigService")
public class ImageConfigServiceImpl implements IImageConfigService {

	@Resource
	private IImageConfigDao imageConfigDao;

	@Override
	public PageInfo<ImageConfig> jdbcFindPageInfo(Long dockerImageTagId, int currentPage, int perPageNum) {
		PageInfo<ImageConfig> page = imageConfigDao.jdbcFindPageInfo(dockerImageTagId, currentPage, perPageNum);
		return page;
	}

	@Override
	public void addImageConfig(ImageConfigDto image) {
		ImageConfigSet ics = new ImageConfigSet();
		JSONArray ja = (JSONArray) JSONArray.toJSON(image.getConfig());
		ics.setConfig(ja.toJSONString());
		ics.setConfigName(image.getConfigName());
		ics.setConfigVersion(image.getConfigVersion());
		ics.setDockerImageTagId(Long.valueOf(image.getDockerImageTagId()));
		Long id = WebUtils.getCtgUser().getSysUserId();
		ics.setCreateUserId(id);
		ics.setLastUpdateUserId(id);
		imageConfigDao.addImageConfig(ics);

	}

	@Override
	public void delImageConfig(Long id) {
		ImageConfig ic = new ImageConfig();
		ic.setId(id);
		imageConfigDao.deleteByPrimaryKey(ic);
	}

	@Override
	public void updateByPrimaryKey(ImageConfigDto icd) {
		ImageConfigSet ics = new ImageConfigSet();
		JSONArray ja = (JSONArray) JSONArray.toJSON(icd.getConfig());
		ics.setId(Long.valueOf(icd.getId()));
		ics.setConfig(ja.toJSONString());
		ics.setConfigName(icd.getConfigName());
		ics.setConfigVersion(icd.getConfigVersion());
		ics.setDockerImageTagId(Long.valueOf(icd.getDockerImageTagId()));
		Long id = WebUtils.getCtgUser().getSysUserId();
		ics.setLastUpdateUserId(id);
		imageConfigDao.updateByPrimaryKey(ics);

	}

	@Override
	public ImageConfig selectByPrimaryKey(ImageConfig imageConfig) {

		return imageConfigDao.selectByPrimaryKey(imageConfig);
	}

	@Override
	public boolean checkImageConfigVersion(Long dockerImageTagId, String configVersion,String configName) {

		List<ImageConfig> list = imageConfigDao.checkImageConfigVersion(dockerImageTagId, configVersion,configName);

		if (null != list && list.size() > 0) {
			return false;
		}

		return true;
	}

	@Override
	public List<ImageConfigName> queryImageConfigNameByImageId(Long dockerImageTagId) {
		List<ImageConfigName> list = imageConfigDao.queryImageConfigNameByImageId(dockerImageTagId);

		return list;
	}

	@Override
	public List<ImageConfigVersion> queryImageConfigVersion(Long dockerImageTagId, String configName) {

		return imageConfigDao.queryImageConfigVersion(dockerImageTagId, configName);
	}

	@Override
	public List<ImageConfig> queryImageConfigByVersion(Long dockerImageTagId, String configName, String configVersion) {
	
		return imageConfigDao.queryImageConfigByVersion(dockerImageTagId, configName, configVersion);
	}


	@Override
	public PageInfo<ImageConfig> jdbcFindPageInfo(String configName, Long dockerImageTagId, int pageNum,
			int pageSize) {
		PageInfo<ImageConfig> page = imageConfigDao.jdbcFindPageInfo(configName,dockerImageTagId, pageNum, pageSize);
		return page;
	}

}
